package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;


import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DateRegUtil;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.iems.dao.VclRoadStatDAO;

public class VclRoadStatService {
	
	private VclRoadStatDAO vclRoadStatDAO;

	public VclRoadStatDAO getVclRoadStatDAO() {
		return vclRoadStatDAO;
	}

	public void setVclRoadStatDAO(VclRoadStatDAO vclRoadStatDAO) {
		this.vclRoadStatDAO = vclRoadStatDAO;
	}

	/**
	 * 路况值变化统计开始
	 */
	public void startWork(String handStatTime) {
		String statTime = "";
		if(StringUtil.isValid(handStatTime)){
			statTime = handStatTime;
		} else {
			//统计的数据日期
			 statTime = DateUtil.addDay(-1);
		}
		String isbakTable="";
		try {
			isbakTable = DynamicReadConfig.getConfigProperty("config.properties", "VclRoadChange");
		} catch (ConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("路况值变化统计作业开始,统计的数据日期:"+statTime+"——————————————"+DateUtil.toString(new Date()));
		//新增日表
		//校验日表是否存在
		boolean isExt = this.vclRoadStatDAO.isExtTab("RoadChangeStat_"+statTime.replace("-", "")+"");
		if(isExt){
			//清空表数据
			this.vclRoadStatDAO.truncateTable("RoadChangeStat_"+statTime.replace("-", "")+"");
		} else {
			//创建表
			this.vclRoadStatDAO.createDayTable(statTime);
		}
		if(isbakTable.equals("1")){
			//创建表
			this.vclRoadStatDAO.createDayBakTable(statTime);
		}
		//获取符合条件的设备(路况统计用)
		List<Map<String, Object>> listVcl = this.vclRoadStatDAO.getVclInfo();
		//初始化表数据
		this.vclRoadStatDAO.initTableData(statTime,isbakTable);
		//sql集合
		List<String> sqlList = new ArrayList<String>();
		//参数集合
		List<Object[]> paramList = new ArrayList<Object[]>();
		//循环，获取每台设备数据	
		if(listVcl!=null && listVcl.size()>0){
			 for(Map<String, Object> mapVcl : listVcl){
				//平原条目数
				int plainNum =0;
				//丘陵条目数
				int hillNum =0;
				//山区条目数
				int mareaNum =0;
				//高原条目数	
				int highNum =0;
				//每个设备总条目数
				int totalNum = 0;
				//获取设备的海拔高度，按照设备ID,位置时间排序
				List<Map<String, Object>> listdataMap = new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> listMap = this.vclRoadStatDAO
						.queryVclAtdeInfo(statTime,MapGetter.getString(mapVcl, "VehicleID"));
				
				if(listMap!=null && listMap.size()>0){
					String gpsOthers[];
					for(Map<String,Object> maps : listMap){
						//第一个点
						listdataMap.add(maps);
						String gpsOthersData[];
						if(StringUtil.isValid(MapGetter.getString(maps, "other"))){
							gpsOthers = MapGetter.getString(maps, "other").split(";");
							for(int i = 0;i<gpsOthers.length;i++){
								gpsOthersData = gpsOthers[i].split(",");
								//其余点
								maps.put("VclP_Altitude", gpsOthersData[3]);
								listdataMap.add(maps);
							}
						}
					}
				}
				
				// 源数据为空，结束统计;不为空，继续
				if (listdataMap != null && listdataMap.size()>0) {
						totalNum = listdataMap.size();
						for(int i = 0;i<listdataMap.size();i++){
							//如果是最后一条，只需判断海拔高度条件
							if(i == listdataMap.size()-1){
								int thisAtd = Integer.parseInt(MapGetter.getString(listdataMap.get(i), "VclP_Altitude"));
								//平原
								if(thisAtd<200){
									plainNum++;
								//丘陵
								} else if(thisAtd<=500 && thisAtd>=200){
									hillNum++;
								//山区
								} else if(thisAtd<=1000 && thisAtd>500){
									mareaNum++;
								//高原	
								} else if(thisAtd>1000){
									highNum++;
									
								}
							}
							//存在下一条
							if(i+1<listdataMap.size()){
								int thisAtd = Integer.parseInt(MapGetter.getString(listdataMap.get(i), "VclP_Altitude"));
								int nextAtd = Integer.parseInt(MapGetter.getString(listdataMap.get(i+1), "VclP_Altitude"));
								//根据本条海拔高度、高度差判断
								//平原
								if(thisAtd<200 && (Math.abs(nextAtd-thisAtd))<100){
									plainNum++;
								//丘陵
								} else if(thisAtd<=500 && thisAtd>=200 && (Math.abs(nextAtd-thisAtd))>=100 && (Math.abs(nextAtd-thisAtd))<=200){
									hillNum++;
								//山区
								}else if(thisAtd<=1000 && thisAtd>500 && (Math.abs(nextAtd-thisAtd))>200 ){
									mareaNum++;
								//高原
								}else if(thisAtd>1000){
									highNum++;
								}
								
							}
							
						}
	
				} else {
					System.out.println("路况值变化统计作业———————设备"+MapGetter.getString(mapVcl, "VehicleNumber")+"源数据为空，结束统计———————"+DateUtil.toString(new Date()));
				}

				sqlList.add(" update RoadChangeStat_"+statTime.replace("-", "")+" set rcs_plain = ?,rcs_hill = ?,rcs_marea = ?,rcs_highland = ?,rcs_tonums=? " +
						" where rcs_stattime=? and rcs_vclid=? ");
				paramList.add(new Object[] {
						plainNum,
						hillNum,
						mareaNum,
						highNum,
						totalNum,
						statTime,
						MapGetter.getString(mapVcl, "VehicleID")});
			 }
			//批量将数据插入统计表
				try {
					 this.vclRoadStatDAO.insertAllToTable(sqlList, paramList,isbakTable);
				} catch (Exception e) {
					System.out.println("执行路况值变化批量数据插入时发生错误！");
				}
		} else {
			System.out.println("没有符合条件的设备！");
		}
		
		System.out.println("路况值变化统计作业结束,路况部分统计结束！");
		/**------------------------------------统计设备进出围栏次数及始发地--------------------------------------------------**/
		int upFenflag = this.vclRoadStatDAO.updateFenNums(statTime);
		System.out.println("路况值变化统计作业结束,统计设备进出围栏次数及始发地部分统计结束！");
		/**-----------------------------------统计设备故障等级次数----------------------------------------------------**/
		int upFltflag = this.vclRoadStatDAO.updateFltNum(statTime);
		System.out.println("路况值变化统计作业结束,统计设备故障等级次数部分统计结束！");
		System.out.println("路况值变化统计作业结束,统计的数据日期:"+statTime+"——————————————"+DateUtil.toString(new Date()));
		
		
	}
	
	/**
	 * 删除指定表数据
	 * @param statTime
	 */
	public void deleteDayTableData(String statTime){
		this.vclRoadStatDAO.deleteDayTableData(statTime);
	}
	public static void main(String[] args) {
		for(int j = 2;j<30;j++){
			System.out.println(DateUtil.addDay(-j));
		}
			String statTime = DateUtil.addDay(-13);
			System.out.println(statTime);
		System.out.println(DateRegUtil.getDaysOfTimeDef(new String[]{"2015-01-10","2015-01-05"}));
		System.out.println(Double.parseDouble(String.valueOf(2))/Double.parseDouble(String.valueOf(5)));
	}
}
